import { cloneDeep } from 'lodash-es'

import { getUnReadNoticesApi, setAllReadNoticesApi, setReadNoticesApi } from '@/api/app/notice'
import LocalCachePack from '@/components/LocalCachePack'

/**
 * @typedef {Object} state
 * @property {boolean} [noticeOpen] 消息推送是否开启
 * @property {boolean} [voiceReminder] 语音提示是否开启
 * @property {boolean} [firstInactivePlayAudio] 是否激活语音提示
 * @property {boolean} [messagePopVisible] 消息中心弹框是否显示
 * @property {{}} [unReadNoticesData] 消息数据
 */

/**
 * @type {state}
 */
const initialState = {
  noticeOpen: import.meta.env.MODE === 'production',
  voiceReminder: LocalCachePack.getLastValue('voiceReminder', false),
  firstInactivePlayAudio: false,
  messagePopVisible: false,
  unReadNoticesData: {
    bannerNoticeList: [],
    redPointNoticeList: [],
    popUpNoticeList: [],
    pageSize: 10,
    current: 1,
  },
}

const namespace = 'notice'

let watchUnReadNoticesTimer

export default {
  namespace,
  state: cloneDeep(initialState),
  effects: {
    *startWatchUnReadNotices(_, { toAction, select }) {
      /** @type {state} */
      const state = yield select(store => store[namespace])
      const { noticeOpen } = state
      watchUnReadNoticesTimer && clearTimeout(watchUnReadNoticesTimer)
      if (!noticeOpen) {
        return
      }
      watchUnReadNoticesTimer = setInterval(() => {
        toAction('pullUnReadNotices')
      }, 10 * 1000)
    },
    *stopWatchUnReadNotices(_, { select }) {
      const { isLogin } = yield select(store => store.app)
      if (!isLogin) {
        return
      }
      clearTimeout(watchUnReadNoticesTimer)
      getUnReadNoticesApi.cancel()
    },
    *pullUnReadNotices({ payload = {} }, { select, call, update }) {
      const { isLogin } = yield select(store => store.app)
      const { unReadNoticesData = {} } = yield select(store => store[namespace])
      if (!isLogin) {
        return
      }
      const { pageSize: oldSize, current: oldOffset } = unReadNoticesData
      const pageConfig = { current: payload.current || oldOffset, pageSize: payload.pageSize || oldSize }
      const pageParams = Object.assign({ read: false }, payload, pageConfig)
      const { data } = yield call(getUnReadNoticesApi, pageParams)
      const { bannerNoticeList = [], redPointNoticeList = {}, popUpNoticeList = [] } = data
      const { content = [], total = 0 } = redPointNoticeList
      const state = {
        current: pageParams.current,
        pageSize: pageParams.pageSize,
        popUpNoticeList,
        bannerNoticeList,
        redPointNoticeList: content,
        total,
      }
      yield update({ unReadNoticesData: state })
    },
    *setReadStatus({ payload }, { call }) {
      yield call(setReadNoticesApi, payload)
      // yield toAction('pullUnReadNotices')
    },
    *setAllReadStatus(_, { call }) {
      yield call(setAllReadNoticesApi)
      // yield toAction('pullUnReadNotices')
    },
  },
  reducers: {
    updateState(state, { payload = {} }) {
      return {
        ...state,
        ...payload,
      }
    },
  },
}
